<?php
/*测试环境*/
class ome_ctl_admin_otherapi extends base_controller
{

	function otherapi()
	{
		// echo json_encode(array(
  //                   "sub_message"=>"Illegal request",
  //                   "flag"=>"failure",
  //                   "sub_code"=>"sign-check-failure",
  //                   )
  //               );


		$data=array_merge($_GET,$_POST);
		$body=file_get_contents("php://input");
		$body = json_decode($body,true);
		$data['api_data']=$body['api_data'];
		error_log(print_r($data,1),3,DATA_DIR . '/otherapi' . date('Y-m-d') . '.log'); 

		try{

			$webservice_url = "http://172.27.32.23:8089/AXToOMs.asmx?wsdl";
			$client = new SoapClient($webservice_url);
			$res = "";
			switch ($data['api_method']) {
				case 'CreateSalesOrder':
					$result = json_decode($data['api_data'],true);
					$param['str'] = $result['str'];
					$param['company'] = $result['company'];
					$res = $client->__soapCall('CreateSalesOrder',array('parameters'=>$param));
					echo json_encode(array('data'=>$res->CreateSalesOrderResult));
					break;
				case 'CreateReturnSalesOrder':
					$result = json_decode($data['api_data'],true);
					$param['str'] = $result['str'];
					$param['company'] = $result['company'];
					$res = $client->__soapCall('CreateReturnSalesOrder',array('parameters'=>$param));
					echo json_encode(array('data'=>$res->CreateReturnSalesOrderResult));
					break;
				case 'SafeInventOMS':
					$result = json_decode($data['api_data'],true);
					$param['list'] = $result['list'];
					$param['company'] = $result['company'];
					$res = $client->__soapCall('SafeInventOMS',array('parameters'=>$param));
					echo json_encode(array('data'=>$res->SafeInventOMSResult));
					break;
				case 'InsSalesFreight':
					$result = json_decode($data['api_data'],true);
					$param['list'] = $result['list'];
					$param['company'] = $result['company'];
					$res = $client->__soapCall('InsSalesFreight',array('parameters'=>$param)); 
					echo json_encode(array('data'=>$res->InsSalesFreightResult));
					break;
				case 'OMS_RequestBilling': //发票过数接口
					$result = json_decode($data['api_data'],true);
					$param['_company'] = $result['company'];
					$param['_taxInfo'] = $result['json'];
					$res = $client->__soapCall('OMS_RequestBilling',array('parameters'=>$param)); 
					echo json_encode(array('data'=>$res->OMS_RequestBillingResult));
					break;
				case 'PromotionalDifference':
					$result = json_decode($data['api_data'],true);
					$param['json'] = $result['json'];
					$param['company'] = $result['company'];
                	$webservice_url = "http://172.27.32.23:8089/AXToOMs.asmx?wsdl";
                	$client = new SoapClient($webservice_url);
                	$res = $client->__soapCall('PromotionalDifference', array('parameters' => $param));
    				error_log('发送数据/' . var_export($param, true)."===".var_export($res), 3, DATA_DIR . '/PromotionalDifference' . date('Y-m-d') . '.log'); 
					echo json_encode(array('data'=>$res->PromotionalDifferenceResult));
					break;
				case 'syncSalesIntegralDeduction':
					$result = json_decode($data['api_data'],true);
					$param['json'] = $result['json'];
					$param['company'] = $result['company'];
                	$webservice_url = "http://172.27.32.23:8089/AXToOMs.asmx?wsdl";
                	$client = new SoapClient($webservice_url);
                	$res = $client->__soapCall('syncSalesIntegralDeduction', array('parameters' => $param));
					echo json_encode(array('data'=>$res->syncSalesIntegralDeductionResult));
					break;
				case 'syncSalesExpenses':
					$result = json_decode($data['api_data'],true);
					$param['expList'] = $result['expList'];
					$param['company'] = $result['company'];
                	$webservice_url = "http://172.27.32.23:8089/AXToOMs.asmx?wsdl";
                	$client = new SoapClient($webservice_url);
                	$res = $client->__soapCall('syncSalesExpenses', array('parameters' => $param));
    				error_log('123发送数据/' . var_export($param, true)."===".var_export($res), 3, DATA_DIR . '/syncSalesExpenses' . date('Y-m-d') . '.log'); 
					echo json_encode(array('data'=>$res->syncSalesExpensesResult));
					break;
				case 'PublishDeliveryInfo':
					$webservice_url = "http://172.27.32.23:8089/ExternalToESB.asmx?wsdl";
                    $client = new SoapClient($webservice_url);
					$result = json_decode($data['api_data'],true);
					$param['SourceId'] = $result['SourceId'];
					$param['DeliveryNoteNo'] = $result['DeliveryNoteNo'];
					$param['OrderNo'] = $result['OrderNo'];
					$param['CourierNumber'] = $result['CourierNumber'];
					$param['ExpressCompany'] = $result['ExpressCompany'];
					$res = $client->__soapCall('PublishDeliveryInfo',array('parameters'=>$param));
					echo json_encode(array('data'=>$res->PublishDeliveryInfoResult));
					break;
				case 'TestWebSerMethod':
					$result = json_decode($data['api_data'],true);
					$param['str'] = $result['str'];
					$res = $client->__soapCall('TestWebSerMethod',array('parameters' => $param));
					echo json_encode(array('data'=>$res->TestWebSerMethodResult));
					break;	
				case 'OmsSendWeiXin':
					//add by ldward oms未过数发送信息到微信企业号中
					$webservice_url = "http://172.23.3.21/liwen_zaozhi_test/io/liwen/omsax.php";

					$result = json_decode($data['api_data'],true);
					$param['data']=json_encode($result['data']) ;
					$param['busdate']=$result['busdate'];
					$res=$this->doCurlPostRequest($webservice_url,$param);
					echo json_encode(array('data'=>$res));
					break;
				case 'OmsGetAlipay':
					//add by ldward 通过奇门接口获取支付宝账单信息
					$webservice_url = "http://172.23.5.171/alipay.php";

					$result = json_decode($data['api_data'],true);
					$param['bill_date']=$result['bill_date'];
					$res=$this->doCurlPostRequest($webservice_url,$param);
					echo json_encode(array('data'=>$res));
					break;	
				case 'OmsImportAlipay':
					//add by ldward 通过奇门接口获取支付宝账单信息
					$webservice_url = "http://172.23.5.171/getalipay.php";

					$result = json_decode($data['api_data'],true);
					$param['fileurl']=$result['fileurl'] ;
					$res=$this->doCurlPostRequest($webservice_url,$param);
					echo json_encode(array('data'=>$res));
					break;
				default:
					echo json_encode(array(
				       "sub_message"=>"Illegal request",
				       "flag"=>"failure",
				       "sub_code"=>"sign-check-failure",
				       )
					);
					break;
			}
            //error_log(print_r($res,1),3,'otherapi.log');
		}
		catch (Exception $e) {
            //error_log(print_r($e->getMessage(),1),3,'otherapi.log');
			echo json_encode(array('data'=>$e->getMessage()));
		}
	}
	/**
	** @desc 封装 curl 的调用接口，post的请求方式
	**/
	function doCurlPostRequest($url,$post_data,$timeout = 5)
	{
	 	$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch, CURLOPT_TIMEOUT,(int)$timeout);
        $output = curl_exec($ch);
        curl_close($ch);

        //打印获得的数据
        header('Content-Type:text/html; charset=utf-8');
        return ((($output)));
	}

}


